﻿using Models.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace yymbDAL
{
    public class My_home
    {
        /// <summary>
        /// 我的首页侧边个人信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Information Side_info(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var p = db.Information.SingleOrDefault(n => n.UserID == id);
                return p;
            }
        }
        /// <summary>
        /// 用户关注数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int use_atte(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.User_rs.Where(n => n.UserID == id).Count();
                return data;
            }
        }
        /// <summary>
        /// 用户粉丝数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int use_fans(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.User_rs.Where(n => n.Use_UserID == id).Count();
                return data;
            }
        }
        /// <summary>
        /// 用户微博数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int use_mibls(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.Microblog.Where(n => n.UserID == id).Count();
                return data;
            }
        }
        /// <summary>
        /// 热门话题
        /// </summary>
        /// <returns></returns>
        //public dynamic hot_topic()
        //{
        //    return data;
        //}
        /// <summary>
        /// 用户首页发微博
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Microblog(int uid, string content, string url, List<WB_pic> pic)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Microblog mb = new Microblog
                {
                    Type = "原创",
                    Content = content,
                    Url = url,
                    Release_time = Convert.ToDateTime(time),
                    UserID = uid,
                    FwID = null,
                    WB_pic=pic
                    
                };
                db.Microblog.Add(mb);
                db.SaveChanges();
                //Up_pic(pic);
                return true;
            }
        }
        //public void Up_pic(List<string> pic)
        //{
        //    using (yymbDBEntities db = new yymbDBEntities())
        //    {
        //        int MbId = db.Microblog.OrderByDescending(m => m.Release_time).Select(m => m.MbID).FirstOrDefault();
        //        foreach (var item in pic)
        //        {
        //            WB_pic wp = new WB_pic
        //            {
        //                MbID = MbId,
        //                URL = item
        //            };
        //            db.WB_pic.Add(wp);
        //        }
        //        db.SaveChanges();
        //    }
        //}
        /// <summary>
        /// 用户微博(全部)
        /// </summary>
        /// <returns></returns>
        public List<Microblog> Oth_use_mibl(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.Microblog.Include("Information").Include("Comment").Where(n => n.UserID == id).OrderByDescending(n => n.Release_time).ToList();

                return data;
            }
        }
        /// <summary>
        /// 相互关注的微博
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Microblog> Mut_atte(int id)
        {
            List<int> li = Mut_atte_list(id);
            List<Microblog> lm = new List<Microblog>();
            foreach (var item in li)
            {
                lm.Add(Mut_atte_select(item));
            }

            return (List<Microblog>)lm.OrderByDescending(n => n.Release_time);

            //using (yymbDBEntities db = new yymbDBEntities())
            //{
            //    StringBuilder str = new StringBuilder("2123");
            //    str.Append("111");
            //    foreach (var item in Mut_atte_bool(id))
            //    {
            //        string stra = @"select i.Nickname,i.Photo,mb.Content,mb.Release_time from [User] as u" +
            //        "left join Microblog as mb on mb.UserID=u.UserID" +
            //        "left join Information as i on i.UserID=u.UserID" +
            //        "left join User_rs as ur on ur.UserID=u.UserID" +
            //        "where u.UserID=" + id + " order by mb.Release_time";

            //    }

            //    List<Class.Mut_atte> mut = new List<Class.Mut_atte>();
            //    foreach (var item in Mut_atte_bool(id))
            //    {
            //        List<Class.Mut_atte> data = Mut_atte_select(item);
            //        mut.Add(data);
            //    }

            //    return data;
            //}
        }
        //按发出时间排序

        //查询数据库
        public Microblog Mut_atte_select(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.Microblog.Include("Information").Include("User_rs")
                    .Where(n => n.UserID == id).OrderByDescending(n => n.Release_time).Take(10);

                return (Microblog)data;
            }
        }
        //相互关注人的id
        public List<int> Mut_atte_list(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var dataI = db.User_rs.Where(n => n.UserID == id).Select(n => new { Hid = n.Use_UserID });//我关注的人
                var dataH = db.User_rs.Where(n => n.Use_UserID == id).Select(n => new { Iid = n.UserID });//关注我的人
                List<int> ids = new List<int>();
                foreach (var itemI in dataI)
                {
                    foreach (var itemH in dataH)
                    {
                        if (itemI.Hid == itemH.Iid)
                        {
                            ids.Add((int)itemI.Hid);
                        }
                    }

                }
                return ids;
            }
        }
        /// <summary>
        /// 用户微博（其他的分类）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public Microblog Oth_use_mibl(int id, string type)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.Microblog.Include("Information").Where(n => n.UserID == id && n.Type == type).OrderByDescending(n => n.Release_time).Take(10);
                return (Microblog)data;
            }
        }
        /// <summary>
        /// 用户微博搜索（关注的人说的话）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public List<Microblog> Mibl_sear(int id, string str)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                List<int?> li = Mibl_sear_list(id);
                List<Microblog> lm = new List<Microblog>();
                foreach (var item in li)
                {
                    foreach (var ite in Mibl_sear_select(item, str))
                    {
                        lm.Add(ite);
                    }
                    
                }

                return lm.OrderByDescending(n => n.Release_time).ToList();
            }
        }
        //查询数据库
        public  List<Microblog> Mibl_sear_select(int? id, string str)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.Microblog.Include("Information")
                    .Where(n => n.UserID == id && n.Content.Contains(str)).Take(10).ToList();
                return data;
            }
        }
        //我关注的人
        public List<int?> Mibl_sear_list(int? id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var dataI = db.User_rs.Where(n => n.UserID == id).Select(n => new { Hid = n.Use_UserID });//我关注的人
                List<int?> li = new List<int?>();
                foreach (var item in dataI)
                {
                    li.Add(item.Hid);
                }
                return li;
            }
        }
        /// <summary>
        /// 用户微博搜索(高级搜索)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <param name="str"></param>
        /// /// <param name="before"></param>
        /// /// <param name="now"></param>
        /// <returns></returns>
        public List<Microblog> Mibl_sear_Adva(int uid,int[] type, string str, DateTime before, DateTime now)
        {
            
            using (yymbDBEntities db = new yymbDBEntities())
            {
                //var data = from n in db.Microblog.Include("Information").Include("User_rs") select n;
                //查询出关注的人
                List<int?> sear_list = Mibl_sear_list(uid);
                int i = sear_list.Count();
                List<Microblog> data = Mibl_sear_Adva(uid,str,before,now);
                //是否包含原创微博
                if (type[0] != 1)
                {
                    data.RemoveAll(p => p.Type == "原创");
                }
                //是否包含转发微博
                if (type[1] != 1)
                {
                    data.RemoveAll(p => p.Type == "转发");
                }
                //是否是纯文字微博
                if (type[2] != 1)
                {
                    data.RemoveAll(p => p.Url != null || !p.Content.Contains("[" + "]"));
                }
                //是否是含图片的微博
                if (type[3] != 1)
                {
                    data= bool_pic(data);
                    //data = data.Where(p=>p.Url.Contains(".jpg"+".jpeg"+".png"));
                }
                //是否含视频
                if (type[4] != 1)
                {
                    data.RemoveAll(p=>p.Url.Contains(".mp4"));
                }
                //是否含音乐
                if (type[5] != 1)
                {
                    data.RemoveAll(p => p.Url.Contains(".mp3"));
                }
                return data;
            }
        }
        //关注的人的微博
        public List<Microblog> Mibl_sear_Adva(int id, string str, DateTime before, DateTime now)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                List<int?> li = Mibl_sear_list(id);
                List<Microblog> lm = new List<Microblog>();
                foreach (var item in li)
                {
                    lm.Add(Mibl_sear_select_Adva(item));
                }
                if (str != null)
                {
                    lm.Where(p => p.Content.Contains(str));
                }
                if (before != null)
                {
                    lm.Where(p => p.Release_time >= before && p.Release_time <= now);
                }
                return (List<Microblog>)lm.OrderByDescending(n => n.Release_time);
            }
        }
        //查询数据库
        public Microblog Mibl_sear_select_Adva(int? id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.Microblog.Include("Information").Include("User_rs")
                    .Where(n => n.UserID == id).OrderByDescending(n => n.Release_time).Take(10);
                return (Microblog)data;
            }
        }
        //判断微博是否有图片
        public List<Microblog> bool_pic( List<Microblog> lm)
        {
            using (yymbDBEntities db=new yymbDBEntities())
            {
                List<int> li = new List<int>();
                foreach (var item in lm)
                {
                    li.Add(item.MbID);
                }
                foreach (var item in li)
                {
                    var data = db.WB_pic.SingleOrDefault(p => p.MbID == item);
                    if (data!=null&&lm.SingleOrDefault(p => p.MbID == item)!=null)
                    {
                        lm.Remove(db.Microblog.SingleOrDefault(p => p.MbID == item));
                    }
                }
                return lm;
            }
        }
        /// <summary>
        /// 查询出来的微博条数
        /// </summary>
        /// <param name="lm"></param>
        /// <returns></returns>
        public int GetMbPage(List<Microblog> lm)
        {
            int page = lm.Count();
            return page;
        }
    }
}
